
/* Generated by Interface Builder */

#import "Arith.h"
#import "Instrum.h"
#import <appkit/graphics.h>
#import "Statement.h"

@implementation Arith

+ initialize
{
   	addimage = [NXImage findImageNamed:"Add"];
   	subimage = [NXImage findImageNamed:"Sub"];
   	multimage = [NXImage findImageNamed:"Mult"];
   	divimage = [NXImage findImageNamed:"Div"];
	in0Offset.x = 20;
	in0Offset.y = 47;
	in1Offset.x = 30;
	in1Offset.y = 47;
	outOffset.x = 28;
	outOffset.y = 17;
	
	return self;
}

- init
{
	written = NO;
	size.width = 80.;
	size.height = 80.;
	center.x = size.width/2.;
	center.y = size.height/2.;
	strcpy(type,"Add");
	strcpy(name,"add");
	
	in0 = [[Param alloc] init:self :&in0Offset];
	in1 = [[Param alloc] init:self :&in1Offset];
	out = [[Param alloc] init:self :&outOffset];

	[in0 setTitle:"Inp1:"];
	[in1 setTitle:"Inp2:"];
	[out setTitle:"Out:"];

	paramList = [[List alloc] initCount:4];
	[paramList addObject:in0];
	[paramList addObject:in1];
	[paramList addObject:out];
	
	[Inst putUgenInList:self];
	
	return self;
}

- remove
{
	id cp,param;
	int i;
							// make sure not connected first 
	for(i = 0; i < ([paramList count]); i++) {	
		param = [paramList objectAt:i];
		if(cp = [param getConnectedParam]) 
			return 0;
	}
							// remove ugen from list
	[Inst removeUgenFromList:self];
	return self;
}

- (NXImage *)getImage
{						// get the right image
	return image;
}

- move:(NXPoint *)newloc
{
	location = *newloc;
	[in0 move:&location];
	[in1 move:&location];
	[out move:&location];
	return self;
}

- setAtype:(char)op
{
	operator = op;
	switch(operator) {
		case '+': {
			image = addimage;
			break;
		}
		case '-': {
			image = subimage;
			break;
		}
		case '*': {
			image = multimage;
			break;
		}
		case '/': {
			image = divimage;
			break;
		}
	}
	return self;
}

- findParamAtPoint:(NXPoint *)point
{
	NXRect *rect;
	int i;
	id param;
	
	for(i = 0; i < [paramList count]; i++) {
		param = [paramList objectAt:i];
		rect = [param getRect];
		if(NXMouseInRect(point,rect,NO))
			return param;
	}
	//printf("no param found\n");
	return nil;
}

- writeUgen
{
	/* 	for each input param:
			see if there is input ugen
				if so, grab the output (ug?) and multiply it by this
					param value
				if not just use the param value
	 	after all params done, write relevant code into lists
			(declarations, assignments and loop statements)
			i.e. ug? = oscil(,,,,);
		go to output param connected ugen and call its "writeUgen"
	*/
	int i;
	int parent[3];
	id param;
	id ug;
	id nupar;
	char str[50];
	char par[20];
	
	//printf("writing ugen %d\n",index);
	sprintf(str,"\tfloat ug%d;\n",index);
	[Inst putVarInList:str];
	sprintf(str,"\tint in0%d;\n",index);
	[Inst putVarInList:str];
	sprintf(str,"\tint in1%d;\n",index);
	[Inst putVarInList:str];
	sprintf(str,"\tfloat out%d;\n",index);
	[Inst putVarInList:str];


	for(i = 0; i < ([paramList count]-1); i++) {	
		param = [paramList objectAt:i];
		if(nupar = [param getConnectedParam]) {
			ug = [nupar getUgen];
			if([ug getWritten] == NO) { 
				parent[i] = [[ug writeUgen] getIndex];
			}
			else  {
				parent[i] = 0;
			}
			//printf("parent[%d] = %d\n",i,parent[i]);
		}
		else 
			parent[i] = 0;
		
	}
	
	sprintf(str,"\tin0%d = %.2f;\n",index, 
		atof([[paramList objectAt:0] getValue]));
	[Inst putAssignInList:str];
	sprintf(str,"\tin1%d = %.2f;\n",index, 
		atof([[paramList objectAt:1] getValue]));
	[Inst putAssignInList:str];

					// output amplitude multiplier:
	strcpy(par,[[paramList objectAt:2] getValue]);
	if(par[0] == 'p' && par[1] == '[')
		sprintf(str,"\tout%d = %s;\n",index,par);
	else
		sprintf(str,"\tout%d = %.2f;\n", index, atof([[paramList objectAt:2] getValue]));
	[Inst putAssignInList:str];

	if(parent[0] && parent[1]) 
		sprintf(str,"\t\tug%d = (ug%d * in0%d %c ug%d * in1%d)*out%d;\n", index, parent[0], index, operator, parent[1], index, index);
	
	else if(parent[0])
		sprintf(str,"\t\tug%d = (ug%d * in0%d %c in1%d)*out%d;\n", index, parent[0], index, operator, index, index);

	else if(parent[1])
		sprintf(str,"\t\tug%d = (in0%d %c  ug%d * in1%d)*out%d;\n", index, index, operator, parent[1], index, index);
	
	else 
		sprintf(str,"\t\tug%d = (in0%d %c in1%d)*out%d;\n", index, index, operator,  index, index);

	[Inst putLoopInList:str];
	written = YES;
	return self;	
}

@end
